<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <!--            <div class="text-center">-->
            <!--              <userAvatar :user="user" />-->
            <!--            </div>-->
            <el-form label-width="120px">
              <!-- 讲师头像 -->
              <el-form-item label="用户头像">
                <!-- 头衔缩略图  -->
                <pan-thumb :image="String(this.user.image)"/>
                <!-- 文件上传按钮 -->
                <el-button type="primary" icon="el-icon-upload"
                           @click="imagecropperShow=true">上传头像</el-button>
                <!--
                v-show：是否显示上传组件
                :key：类似于id，如果一个页面多个图片上传控件，可以做区分
                :url：后台上传的url地址, 这个地方直接是给写死了的
                @close：关闭上传组件的方法
                @crop-upload-success：上传成功后的回调 -->
                <image-cropper
                  v-show="imagecropperShow"
                  :width="300"
                  :height="300"
                  :key="imagecropperKey"
                  :url="'http://localhost:8011'+'/eduoss/fileoss'"
                  field="file"
                  @close="close"
                  @crop-upload-success="cropSuccess1"/>
              </el-form-item>
            </el-form>

            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ user.userName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />手机号码
                <div class="pull-right">{{ user.phone }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />用户邮箱
                <div class="pull-right">{{ user.email }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="peoples" />所属角色
                <div class="pull-right">{{ user.roleGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ user.createTime }}</div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// import userAvatar from "./userAvatar";
import userInfo from "./userInfo";
import resetPwd from "./resetPwd";
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

import { getUserProfile,addImage } from "@/api/user";
export default {
  name: "Profile",
  components: { userInfo, resetPwd, ImageCropper, PanThumb },
  data() {

    return {
      user: {},
      activeTab: "userinfo",
      imagecropperShow:false,
      imagecropperKey:0,     // 上传组件的key
      // BASE_API: '"http://localhost:9001"',
      //BASE_API:process.env.BASE_API, // 获取dev.env.js端口号
    };

  },
  created() {
    this.getUser();
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        //alert("初始图片"+this.user.image)
      });
    },
    // 上传成功后的回调处理
    cropSuccess1(data) {
      this.imagecropperShow=false
      // //上传之后接口返回图片地址
      this.user.image = data
      if (data != null){
        alert("运行到了这里"+data)
        addImage(data).then(response => {
          this.$message.success("上传成功")
        });
      }
      this.imagecropperKey = this.imagecropperKey+1
    },
    //关闭弹窗的方法
    close(){
      this.imagecropperShow = false
      // 上传组件初始化
      this.imagecropperKey = this.imagecropperKey+1
    },
  }
};
</script>
